package com.xgggh.redpack.ui.redpack;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.jakewharton.rxbinding3.view.RxView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseActivity;
import com.xgggh.redpack.databinding.ActivityQrCodeScannerBinding;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import io.reactivex.functions.Consumer;

// https://github.com/bingoogolapple/BGAQRCode-Android/blob/master/zxingdemo/src/main/java/cn/bingoogolapple/qrcode/zxingdemo/TestScanActivity.java

public class QrCodeScannerActivity extends BaseActivity implements QRCodeView.Delegate  {
    ActivityQrCodeScannerBinding mBinding;
    private static final int request_code_camera=11122;
    boolean isFlashOn=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding= DataBindingUtil.setContentView(this,R.layout.activity_qr_code_scanner);
        setSupportActionBar(mBinding.toolbar.toolbar);
        RxView.clicks(mBinding.fab)
                .compose(uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (!isFlashOn){
                            mBinding.contentQrCodeScanner.scanner.openFlashlight();
                        }else{
                            mBinding.contentQrCodeScanner.scanner.closeFlashlight();
                        }
                    }
                });
        mBinding.contentQrCodeScanner.scanner.setDelegate(this);
    }
    long start=System.currentTimeMillis();
    @Override
    protected void onStart() {
        super.onStart();
        start=System.currentTimeMillis();
        Boolean _cameraPass=mayRequestPermissions(new String[]{Manifest.permission.CAMERA},request_code_camera);
        if (_cameraPass){
            mBinding.contentQrCodeScanner.scanner.startCamera();
            mBinding.contentQrCodeScanner.scanner.startSpotAndShowRect();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBinding.contentQrCodeScanner.scanner.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBinding.contentQrCodeScanner.scanner.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (request_code_camera==requestCode && resultCode==RESULT_OK){
            mBinding.contentQrCodeScanner.scanner.startCamera();
            mBinding.contentQrCodeScanner.scanner.startSpotAndShowRect();
        }
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        //getSupportActionBar().setTitle(result);
        long end=System.currentTimeMillis();
        mBinding.contentQrCodeScanner.textView2.setText(mBinding.contentQrCodeScanner.textView2.getText().toString()+"\n"+(end-start)+"ms");
        start=System.currentTimeMillis();
        vibrate();
        mBinding.contentQrCodeScanner.scanner.startSpot();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        isFlashOn=isDark;
            if (isDark){
                mBinding.fab.setImageResource(R.drawable.ic_flash_off_black_24dp);
            }else{
                mBinding.fab.setImageResource(R.drawable.ic_flash_on_black_24dp);
            }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "二维码解码错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

}
